package epin.fujiajun.data.array;

import epin.fujiajun.data.Array;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by DELL on 2017/8/28.
 */
public class Card extends Array<Card> {

    private final static int MIN_NUMBER = 52;

    private Color color;
    private int number;

    public Card(Color color, int number){
        this.color = color;
        this.number = number;
    }

    public Color getColor() {
        return color;
    }

    public int getNumber() {
        return number;
    }

    @Override
    public int compareTo(Card o) {
        if (this.color.val() > o.color.val()) return +1;
        if (this.color.val() < o.color.val()) return -1;
        if (this.number > o.number) return +1;
        if (this.number < o.number) return -1;
        return 0;
    }

    @Override
    public String toString() {
        String s = "";

        switch (this.color){
            case spade:
                s += "黑桃";
                break;
            case heart:
                s += "红心";
                break;
            case club:
                s += "梅花";
                break;
            case diamond:
                s += "方块";
                break;
        }

        switch (this.number){
            case 11:
                s += "J";
                break;
            case 12:
                s += "Q";
                break;
            case 13:
                s += "K";
                break;
            default:
                s += String.valueOf(number);
                break;
        }

        return s;
    }


    public static Card[] createArray(){
        Card[] cards = new Card[MIN_NUMBER];

        List<Integer> numbers = getRandom(MIN_NUMBER);

        for (int i = 0;i < numbers.size();i++) {
            Card card = new Card(getColor(i/13+1), i%13+1);
            cards[i] = card;
        }

        return cards;
    }


    private static List<Integer> getRandom(int size){

        Random random = new Random();
        List<Integer> list = new ArrayList<>();

        int i = 0;
        while (i < size){
            int number = random.nextInt(size) + 1;

            if(!list.contains(number)){
                list.add(number);
                i++;
            }
        }

        return list;
    }


    private static Color getColor(int i){
        switch (i){
            case 1:
                return Color.spade;
            case 2:
                return Color.heart;
            case 3:
                return Color.club;
            case 4:
                return Color.diamond;
            default:
                return null;
        }
    }


    enum Color{

        spade(1),
        heart(2),
        club(3),
        diamond(4);

        private int value;

        Color(int value){
            this.value = value;
        }

        public int val(){
            return value;
        }
    }
}
